﻿/*
创建人：zhunan
日期：2024-07-19
描述：件号搜索下拉组件，工艺搜索下拉组件
*/
(function ($) {
  /* 
  *件号下拉选择组件
  */
  $.fn.productCombo = function (option, param) {
    var comboId = $(this).attr("id");
    //件号组件默认事件
    var productOption = {
      param: { productName: "", productID: "" }, //参数
      search: function (val) { }, //件号查询点击事件(参数为输入框数据)
      cancel: function () { }, //件号取消点击事件
      selected: function (value, text) { } //件号下拉选择事件（参数为选择的下拉数据ID,文本）
    };
    //定义件号下拉事件实现
    var productDefaults = {
      url: getRootPath() + "/Ashx/CommonHandler.ashx", //后台路径
      //初始化页面
      init: function () {
        productDefaults.bind();
        productDefaults.initUI(); //初始化页面控件
        if (isNullOrWhiteSpaceStr(productOption.param.productName) === false) {
          productDefaults.productSearch(productOption.param);
        }
      },
      //页面事件绑定
      bind: function () {
        //件号查询
        $('#btn' + comboId + 'Search').on("click", function () {
          clearMessage();
          var productVal = $('#txt' + comboId).textbox("getValue");
          if (isNullOrWhiteSpaceStr(productVal)) {
            displayMessage("需输入查询条件", false);
            return;
          }
          productDefaults.productSearch({ productName: productVal });
          productOption.search(productVal); //件号查询回调
        });
        //件号查询取消
        $('#btn' + comboId + 'Clear').on("click", function () {
          clearMessage()
          productDefaults.initUI(1);
          productOption.cancel(); //件号取消回调
        });
        $(document).ready(function () {
          //绑定件号下拉数据
          $("#sel" + comboId).combobox({
            valueField: 'PRODUCTID',
            textField: 'PRODUCTNAME',
            panelHeight: 200,
            onSelect: function (record) {
              productOption.selected(record.PRODUCTID, record.PRODUCTNAME); //选择后回调
            }
          });
        });
      },
      //件号查询方法
      productSearch: function (param) {
        $.postAjax({
          url: productDefaults.url,
          data: { action: "GetProductSelect", productName: param.productName },
          ajaxSuccess: function (res) { //ajax成功后
            if (!res.Result) {
              displayMessage(res.Message, res.Result);
              return;
            }
            $('#div' + comboId + 'Txt').hide();
            $('#div' + comboId + 'Select').show();
            $('#div' + comboId + 'Search').hide();
            $('#div' + comboId + 'Clear').show();
            //绑定件号下拉数据
            $("#sel" + comboId).combobox({
              data: res.DBTable, //下拉数据
              value: isNullOrWhiteSpaceStr(param.productID) ? "" : param.productID//add:Wangjh 20230822
            });
          }
        });
      },
      //初始化页面控件
      initUI: function (type) {
        //文本框、搜索按钮显示
        if (type === 1) {
          $("#txt" + comboId).textbox("setValue", "");
          $("#sel" + comboId).combobox('clear');
        }
        $("#div" + comboId + "Txt").show();
        $("#div" + comboId + "Select").hide();
        $("#div" + comboId + "Search").show();
        $("#div" + comboId + "Clear").hide();
      }
    };
    switch (typeof option) {
      case "string":
        if (option === "init") {//恢复初始化状态
          if (document.getElementById("sel" + comboId)) {
            productDefaults.initUI(1);
          }
        }
        else if (option === "setCombo") {
          if (document.getElementById("sel" + comboId)) {
            param = $.extend({}, { productName: "" }, param);
            productDefaults.productSearch(param);
          }
        }
        else if (option === "getValue") {
          if (document.getElementById("sel" + comboId)) {
            return $("#sel" + comboId).combobox('getValue')
          }
        }
        else if (option === "getText") {
          if (document.getElementById("sel" + comboId)) {
            return $("#sel" + comboId).combobox('getText')
          }
        }
        break;
      case "object":
        var strHtml = getProductComboHtml(comboId);
        $(this).html(strHtml);
        productOption = $.extend({}, productOption, option);
        productDefaults.init();
        break;
      case "undefined":
        var strHtml = getProductComboHtml(comboId);
        $(this).html(strHtml);
        productOption = $.extend({}, productOption, option);
        productDefaults.init();
        break;
      default:
        break;
    }
  };
  //件号html控件
  function getProductComboHtml(id) {
    return "<div style='float: left;'>" +
      "<div class='divLabel' style='margin-bottom: 5px'>件号：</div>" +
      "<div id='div" + id + "Txt' style='float: left'>" +
      "<input id='txt" + id + "' class='easyui-textbox' type='text' data-options='' style='width: 350px;height: 24px' /></div>" +
      "<div id='div" + id + "Select' style='float: left'>" +
      "<select style='width: 400px; height: 24px' id='sel" + id + "' data-options='editable:false' class='easyui-combobox'></select>" +
      "</div><div id='div" + id + "Search' style='float: left'>" +
      "<a id='btn" + id + "Search' href='#' class='easyui-linkbutton' data-options=\"iconCls:\'icon-search\'\"></a>" +
      "</div><div id='div" + id + "Clear' style='float: left'>" +
      "<a id='btn" + id + "Clear' href='#' class='easyui-linkbutton' data-options=\"iconCls:\'icon-cancel\'\" style='height: 24px;'></a>" +
      "</div></div>";
  }

  /* 
  *工艺下拉选择组件
  */
  $.fn.workflowCombo = function (option, param) {
    var id = $(this).attr("id");
    //工艺组件默认事件
    var workflowOption = {
      param: { productName: "", workflowName: "", workflowID: "", isThisFactory: false }, //参数 add:Wangjh 20230822 增加workflowID
      search: function (val) { }, //工艺查询点击事件(参数为输入框数据)
      cancel: function () { }, //工艺取消点击事件
      selected: function (value, text) { } //工艺下拉选择事件（参数为选择的下拉数据ID,文本）
    };

    //定义工艺下拉事件实现
    var workflowDefaults = {
      url: getRootPath() + "/Ashx/CommonHandler.ashx", //后台路径
      //初始化页面
      init: function () {
        workflowDefaults.bind();
        workflowDefaults.initUI(); //初始化页面控件
        if (isNullOrWhiteSpaceStr(workflowOption.param.productName) === false || isNullOrWhiteSpaceStr(workflowOption.param.workflowName) === false) {
          workflowDefaults.workflowSearch(workflowOption.param.productName, workflowOption.param.workflowName);
        }
      },
      //页面事件绑定
      bind: function () {
        //工艺查询
        $('#btn' + id + 'Search').on("click", function () {
          clearMessage();
          var workflowVal = $('#txt' + id).textbox("getValue");
          workflowDefaults.workflowSearch("", workflowVal);
          workflowOption.search(workflowVal); //工艺查询回调
        });
        //工艺查询取消
        $('#btn' + id + 'Clear').on("click", function () {
          clearMessage();
          workflowDefaults.initUI(1);
          workflowOption.cancel(); //工艺取消回调
        });
        $(document).ready(function () {
          //绑定工艺下拉数据
          $("#sel" + id).combobox({
            valueField: 'WORKFLOWID',
            textField: 'WORKFLOWNAME',
            panelHeight: 200,
            onSelect: function (record) {
              if (record !== null) {
                workflowOption.selected(record.WORKFLOWID, record.WORKFLOWNAME); //选择回调
              }
            }
          });
        });
      },
      //工艺查询方法
      workflowSearch: function (productname, workflowName) {
        if (isNullOrWhiteSpaceStr(productname) && isNullOrWhiteSpaceStr(workflowName)) {
          displayMessage("需输入查询条件", false);
          return;
        }
        $.postAjax({
          url: workflowDefaults.url,
          data: { action: "GetWorkflowSelect", productName: productname, workflowName: workflowName, isThisFactory: workflowOption.param.isThisFactory },
          ajaxSuccess: function (res) { //ajax成功后
            if (!res.Result) {
              displayMessage(res.Message, res.Result);
              return;
            }
            $('#div' + id + 'Txt').hide();
            $('#div' + id + 'Select').show();
            $('#div' + id + 'Search').hide();
            $('#div' + id + 'Clear').show();
            if (res.DBTable.length === 0) {
              displayMessage("未查询到工艺", false);
              return;
            }
            //绑定工艺下拉数据
            $("#sel" + id).combobox({
              data: res.DBTable, //下拉数据
              value: isNullOrWhiteSpaceStr(workflowOption.param.workflowID) ? res.DBTable[0].WORKFLOWID : workflowOption.param.workflowID //默认第一个或指定的值
            });
          }
        });
      },
      //初始化工艺控件
      initUI: function (type) {
        //文本框、搜索按钮显示
        //工艺路线
        if (type === 1) {
          $('#txt' + id).textbox("setValue", "");
          $('#sel' + id).combobox('clear');
        }
        $('#div' + id + 'Txt').show();
        $('#div' + id + 'Select').hide();
        $('#div' + id + 'Search').show();
        $('#div' + id + 'Clear').hide();
      }
    };
    switch (typeof option) {
      case "string":
        if (option === "init") { //恢复初始化状态
          if (document.getElementById("sel" + id)) {
            workflowDefaults.initUI(1);
          }
        }
        else if (option === "setCombo") {
          if (document.getElementById("sel" + id)) {
            param = $.extend({}, { productName: "", workflowName: "" }, param);
            workflowDefaults.workflowSearch(param.productName, param.workflowName);
          }
        }
        else if (option === "setValue") {
          if (document.getElementById("sel" + id)) {
            if (isNullOrWhiteSpaceStr(param)) {
              return;
            }
            $(document).ready(function () {
              $("#sel" + id).combobox('setValue', param)
            });
          }
        }
        else if (option === "getValue") {
          if (document.getElementById("sel" + id)) {
            return $("#sel" + id).combobox('getValue')
          }
        }
        else if (option === "getText") {
          if (document.getElementById("sel" + id)) {
            return $("#sel" + id).combobox('getText')
          }
        }
        break;
      case "object":
        var strHtml = getWorkflowComboHtml(id);
        $(this).html(strHtml);
        workflowOption = $.extend({}, workflowOption, option);
        workflowDefaults.init();
        break;
      case "undefined":
        var strHtml = getWorkflowComboHtml(id);
        $(this).html(strHtml);
        workflowOption = $.extend({}, workflowOption, option);
        workflowDefaults.init();
        break;
      default:
        break;
    }
  };
  //工艺html控件
  function getWorkflowComboHtml(id) {
    return "<div style='float: left; margin-left: 10px'>" +
      "<div class='divLabel' style='margin-bottom: 5px'>工艺路线：</div>" +
      "<div id='div" + id + "Txt' style='float: left'>" +
      "<input id='txt" + id + "' class='easyui-textbox' type='text' data-options='' style='width: 400px;height: 24px' /></div>" +
      "<div id='div" + id + "Select' style='float: left'>" +
      "<select style='width: 400px; height: 24px' id='sel" + id + "' data-options='editable:false' class='easyui-combobox'></select>" +
      "</div><div id='div" + id + "Search' style='float: left'>" +
      "<a id='btn" + id + "Search' href='#' class='easyui-linkbutton' data-options=\"iconCls:\'icon-search\'\"></a>" +
      "</div><div id='div" + id + "Clear' style='float: left'>" +
      "<a id='btn" + id + "Clear' href='#' class='easyui-linkbutton' data-options=\"iconCls:\'icon-cancel\'\" style='height: 24px'></a>" +
      "</div></div>";

  }

  //自定义ajax（post类型）
  $.postAjax = function (param) {
    var options = {
      type: "Post",
      url: "",
      data: null,
      cashe: false,
      async: false,
      dataType: "json",
      beforeSend: function () {
        $.messager.progress({ text: '', msg: '处理中...' });
      },
      ajaxSuccess: function (data) { },
      success: function (data) {
        $.messager.progress('close');
        options.ajaxSuccess(data);
      },
      ajaxError: function () { },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $.messager.progress('close');
        displayMessage(XMLHttpRequest.responseText, false);
        options.ajaxError();
      }
    };
    options = $.extend({}, options, param);
    $.ajax(options);
  }

})(jQuery)